package com.example.taxfloatproject20240926.ui.room;

import android.content.Context;


import com.example.taxfloatproject20240926.ui.bean.CallBean;

import java.util.List;

import io.reactivex.Completable;
import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class CallRepository {
    private CompositeDisposable compositeDisposable = new CompositeDisposable();
    private Context context;
    CallInterface mCallInterface;

    public CallRepository(Context context) {
        this.context = context;

    }

    public void setCallInterface(CallInterface callInterface) {
        this.mCallInterface = callInterface;
    }

    private  <T> void addDisposable(Completable completable, Action action) {
        compositeDisposable.add(completable.subscribeOn(Schedulers.io()).
                observeOn(AndroidSchedulers.mainThread()).subscribe(action));
    }

    private  <T> void addDisposable(Flowable<T> flowable, Consumer<T> consumer) {
        compositeDisposable.add(flowable
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(consumer));
    }

    public void insertCallData(CallBean calledBean) {
        Completable completable =
                AppDataBase.getInstance(context).callDao().insert(calledBean);
        addDisposable(completable, new Action() {
            @Override
            public void run() throws Exception {
                if (mCallInterface != null) {
                    mCallInterface.addSuccess(true);
                }
            }
        });
    }


    public void deleteAll() {
        Completable completable =
                AppDataBase.getInstance(context).callDao().delete();
        addDisposable(completable, new Action() {
            @Override
            public void run() throws Exception {
                if (mCallInterface != null) {
                    mCallInterface.addSuccess(false);
                }
            }
        });
    }




    public void deleteCallData(CallBean calledBean) {
        Completable completable =
                AppDataBase.getInstance(context).callDao().delete(calledBean);
        addDisposable(completable, new Action() {
            @Override
            public void run() throws Exception {
                if (mCallInterface != null) {
                    mCallInterface.addSuccess(false);
                }
            }
        });

    }

    public void queryAll() {
        Flowable<List<CallBean>> callList =
                AppDataBase.getInstance(context).callDao().getCallList();
        addDisposable(callList, new Consumer<List<CallBean>>() {
            @Override
            public void accept(List<CallBean> calledBeanList) throws Exception {
                if (mCallInterface != null) {
                    mCallInterface.callList(calledBeanList);
                }
            }
        });
    }

}
